/*
* SMILParser
*
* Version 1.0
*
* Programmers: Jesse Louderback, Noah Pham, Chad Carpenter, and Marc Hindman
*/
package Message;

import java.util.ArrayList;
import java.util.Hashtable;

import com.SmilDial.R;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

public class SMILMessage {
	protected SMILMeta meta = new SMILMeta();
	protected Hashtable<String, SMILRegion> regions = new Hashtable<String, SMILRegion>();
	protected ArrayList<SMILAudio> audio = new ArrayList<SMILAudio>();
	protected ArrayList<SMILText> text = new ArrayList<SMILText>();
	protected ArrayList<SMILImage> imgs = new ArrayList<SMILImage>();
	private Bitmap Fin;
	
	private int itemToEdit;
	private boolean edit = false;

	protected float endTime = 0;

	public void addText(SMILText t) {
		text.add(t);
	}

	public void addImage(SMILImage i) {
		imgs.add(i);
	}

	public void addAudio(SMILAudio a) {
		audio.add(a);
	}

	public void addMeta(SMILMeta smil_Meta) {
		meta = smil_Meta;
	}

	public void addRegion(String regName, SMILRegion reg) {
		regions.put(regName, reg);
	}

	public void AddObject(SMILObject obj) {
		if (obj.getType().equals("img")) {
			imgs.add(new SMILImage(obj));
		} else if (obj.getType().equals("text")) {
			text.add(new SMILText(obj));
		} else if (obj.getType().equals("audio")) {
			audio.add(new SMILAudio(obj));
		}
		if ((obj.getStartTime() + obj.getEndTime()) > endTime) {
			this.endTime = obj.getStartTime() + obj.getEndTime();
		}
	}

	public int getItemToEdit() {
		return itemToEdit;
	}

	public void setItemToEdit(String itemToEdit) {
		if (itemToEdit.equals("img")) {
			this.itemToEdit = 0;
		} else if(itemToEdit.equals("text")) {
			this.itemToEdit = 1;
		}
	}

	public boolean getEdit() {
		return edit;
	}

	public void setEdit(boolean edit) {
		this.edit = edit;
	}

	@Override
	public String toString() {
		String message = "";
		
		// main container
		message = "<smil><head>";

		// add meta
		message += meta.toString();

		// add regions
		message += "<layout>";
		for (SMILRegion reg : regions.values()) {
			message += reg.toString();
		}
		message += "</layout></head>";

		// add body
		message += "<body><par>";

		for (SMILAudio aud : audio) {
			message += aud.toString();
		}
		for (SMILImage img : imgs) {
			message += img.toString();
		}
		for (SMILText txt : text) {
			message += txt.toString();
		}
		message += "</par></body></smil>";
		return message;
	}

	public void reset() {
		for (SMILAudio aud : audio) {
			aud.resetAudio();
		}
	}

	public boolean finished(float time) {
		if (time >= endTime) {
			return true;
		} else {
			return false;
		}
	}
	
	public void onDraw(Canvas canvas, float time, Paint paint) {
		for (SMILText txt : text) {
			if ((time >= txt.getStartTime() && time <= txt.getEndTime()) || edit) {
				SMILRegion reg = regions.get(txt.getRegion());
				paint.setTextSize(txt.getTextSize());
				paint.setColor(txt.getColor());
				canvas.drawText(txt.getText(), reg.getX(), reg.getY(), paint);
			}
		}
		for (SMILImage image : imgs) {
			SMILRegion reg = regions.get(image.getRegion());
			if ((time >= image.getStartTime() && time <= image.getEndTime()) || edit) {
				Matrix im = new Matrix();
				im.preScale(reg.getWidth()/image.getBmp().getWidth(), reg.getHeight()/image.getBmp().getHeight());

				Matrix move = new Matrix();
				move.setTranslate(reg.getX(), reg.getY());

				im.postConcat(move);
				canvas.drawBitmap(image.getBmp(), im, paint);
			}
		}
		for (SMILAudio aud : audio) {
			if (time >= aud.getStartTime() && time <= aud.getEndTime() && !edit) {
				aud.playAudio();
			} else {
				aud.stopAudio();
			}
		}
	}

	public void destroy() {
		for (SMILAudio aud : audio) {
			aud.killMediaPlayer();
		}
	}

	public void moveItem(String ob, float dx, float dy){
		if (ob.equals("image")) {
			regions.get(imgs.get(0).getRegion()).move(dx, dy);
		} else if (ob.equals("text")) {
			regions.get(text.get(0).getRegion()).move(dx, dy);
		}
		
	}
	
	public void sizeItem(String ob, float f) {
		if (ob.equals("text")){
			if (f < 0) {
				text.get(0).decTextSize();
			} else {
				text.get(0).incTextSize();
			}
		}
	}

	public void sizeImgItem(float deltaX, float deltaY) {
		regions.get(imgs.get(0).getRegion()).changeSize(deltaX, deltaY);
	}

	public boolean isFinished(float currentTime) {
		if (currentTime >= endTime) {
			return true;
		} else {
			return false;
		}
	}

	public void stopMessage(Canvas canvas, Paint paint) {
		canvas.drawBitmap(Fin, 0, 0, paint);
	}

	public void pauseAudio() {
		for (SMILAudio aud : audio){
			aud.pause();
		}
	}

	public void setFin(Bitmap r) {
		Fin = r;		
	}

	public SMILAudio getLastAudioAdded() {
		return audio.get(audio.size()-1);		
	}


}
